//
//  HZDateFormatter.m
//  testheaderAndfooter
//
//  Created by huazi on 14-3-21.
//  Copyright (c) 2014年 huifu. All rights reserved.
//

#import "HZDateFormatter.h"

@implementation HZDateFormatter
+(NSString *)returnNowDate
{
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
    NSString *returnStr =[dateFormatter stringFromDate:[NSDate date]];
    return returnStr;
}
+(NSString *)returnNowDateType:(NSString *)strType
{
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:strType];
    NSString *returnStr =[dateFormatter stringFromDate:[NSDate date]];
    return returnStr;
}
+(NSString *)returnStrFromSecond:(NSString *)sendTime
{
//    NSDate *dateSend =[NSDate dateWithTimeIntervalSince1970:[sendTime doubleValue]/1000];
//    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
//    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
//    NSString *returnStr =[dateFormatter stringFromDate:dateSend];
//    return returnStr;
    
    NSDate *dateSend =[NSDate dateWithTimeIntervalSince1970:[sendTime doubleValue]/1000];
//    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc]init];
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
//    NSString *returnStr =[dateFormatter stringFromDate:dateSend];
   
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit;
    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:dateSend];
    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if ([cmp1 day] == [cmp2 day]) { // 今天
        formatter.dateFormat = @"今天 HH:mm";
    } else if ([cmp1 year] == [cmp2 year]) { // 今年
        formatter.dateFormat = @"MM-dd HH:mm";
    } else {
        formatter.dateFormat = @"yyyy-MM-dd";
    }
    NSString *time = [formatter stringFromDate:dateSend];
    
 return time;
    
   
    
    
}
+(NSString *)returnSecondNowDateSince1970
{
    NSTimeInterval second =[[NSDate date] timeIntervalSince1970]*1000;
    return [NSString stringWithFormat:@"%.0f",second];
}
+(NSString *)returnSecondNowDateToLaterDate:(NSString *)laterDate
{
    
    NSDateFormatter *dateFormatter =[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSTimeInterval second =[[NSDate date] timeIntervalSinceDate:[dateFormatter dateFromString:laterDate]];
    if (second <0)
    {
       return [NSString stringWithFormat:@"%.0f",second];
    }
    
    return @"0";
}

+(NSString *)returnStrFromSecondSince1970:(double)second andStrType:(NSString *)type
{
     NSDate *dateSend =[NSDate dateWithTimeIntervalSince1970:second/1000];
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = type;
     NSString *time = [formatter stringFromDate:dateSend];
    return time;
}

+(NSString *)returnStrFromNow:(double)second{
  NSTimeInterval second1 =[[NSDate date] timeIntervalSince1970]*1000;
  double spaced = (second1 - second);
    //60s以内 刚刚
    if (spaced<1000*60) {
        return @"刚刚";
    }
    //60m以内 XX分钟前
    else if (spaced>1000*60&&spaced<1000*60*60){
        int i= spaced/(1000*60);
        return [NSString stringWithFormat:@"%d分钟前",i];
    }
    //大于60分小于24小时 XX小时前
    else if (spaced>1000*60*60 && spaced<1000*60*60*24){
        int i= spaced/(1000*60*60);
        return [NSString stringWithFormat:@"%d小时前",i];
    }
    //大于24小时小于365天 XX天前
    else if (spaced>1000*60*60*24 && spaced<(double)1000*60*60*24*365){
        int i= spaced/(1000*60*60*24);
        return [NSString stringWithFormat:@"%d天前",i];
    }
    //大于365天 X年前
    else{
        int i= spaced/(1000*60*60*24*365);
        return [NSString stringWithFormat:@"%d年前",i];
    }
}


@end
